package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.apache.ibatis.cache.decorators.BlockingCache;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //属性拷贝
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //根据ID查数据库，没用就新增

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //2 就是查到的那一条数据

        if (list != null &&list.size() == 1){
            //查到了有数据，那就更新
             shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateNumberById(shoppingCart);

        }else{
            //没查到，那就插入
            if (shoppingCartDTO.getDishId() != null){
                //如果是菜品，那就加上口味

                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setId(dish.getId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());



            }else {
                //如果是套餐，那就插入
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setId(setmeal.getId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);
        }
        //有就更新，update，number字段加一

        //Mapper,一个更新，一个插入，一个查询







    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        return shoppingCartMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }

    public void clean(){
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void subByDishId(ShoppingCartDTO shoppingCartDTO) {
        //判断删除的是菜品还是套餐
        if (shoppingCartDTO.getDishId() != null){
           ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).dishId(shoppingCartDTO.getDishId()).build();
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            shoppingCartMapper.updateNumberById(shoppingCart1);
        }else {
            ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).setmealId(shoppingCartDTO.getSetmealId()).build();
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            shoppingCartMapper.updateNumberById(shoppingCart1);

        }


    }
}
